/*
 * Translator.cpp
 *
 *  Created on: Nov 4, 2011
 *      Author: gonzalo
 */

#include "Translator.h"
#include "../DTOCliente.h"
#include "../DTOEvent.h"
#include "../DTOShipStatus.h"

DTO* Translator::translate(Event& event) {

	switch (event.getType()) {
	case Quit:
		return new DTOCliente(event.getSender(), DISCONECTED,NULL);
		break;
	case KeyPressed:
	case KeyReleased:
		return event.transformInDTO();
		break;
	case UserEvent:
		return translateUserEvent(event);
		break;
	default:
		return NULL;
		break;
	}

	return NULL;

}

DTO* Translator::translateUserEvent(Event& event){
	switch (event.getEvent()->user.code) {
		case Ship_Status:
				return event.transformInDTOShipStatus();
			break;
		default:
			break;
	}

	return NULL;
}

void Translator::translateAndPushEvent(DTO* dto) {

	if (dto == NULL) {
		return;
	}


	switch (dto->getType()) {
		case EVENT:{
			DTOEvent* dtoevent = (DTOEvent*) dto;
			Event event(dtoevent);
			SDL_PushEvent(event.getEvent());
			break;
		}
		case CLIENTE:{
				translateDTOClient((DTOCliente*)dto);
			break;
		}
		case SHIP_STATUS:{
				DTOShipStatus* dtoShip= (DTOShipStatus*)dto;
				Event event(dtoShip);
				SDL_PushEvent(event.getEvent());
			break;
		}
		default:{
			break;
		}
	}

}

void Translator::translateDTOClient(DTOCliente* dto){

	//Init the event
	SDL_Event event;
	event.type = SDL_USEREVENT;

	//CONNECTED, REQ_SYNCRONIZE ,DISCONECTED
	switch (dto->getStatus()) {
		case CONNECTED:
				event.user.code = NewUser;
				event.user.data1 = new std::string(dto->getOwner());
				event.user.data2 = new std::string(dto->getShip()->serializar());
			break;
		case REQ_SYNCRONIZE:
				event.user.code = UserSyncronize;
			break;
		case DISCONECTED:
				event.user.code = UserDisconected;
			break;
	}

	SDL_PushEvent(&event);
}

std::list<Asteroide*> Translator::translateAsteroideDTO(
		std::list<DTOAsteroide*> elements) {
	std::list<Asteroide*> retorno;

	std::list<DTOAsteroide*>::iterator it;
	for (it = elements.begin(); it != elements.end(); ++it) {
		retorno.push_back(new Asteroide((DTOAsteroide*) (*it)));
	}

	return retorno;
}
